using System;
using Spread.Common.Features.Sample;
using Spread.Common.DataStore;

namespace Spread.Search
{
    public partial class Introduction : BaseSampleControl
    {
        public Introduction()
        {
            InitializeComponent();
            Name = "Introduction";
            //Initialize Spread.
            var dataSource = new DataService().AccessDataSource;
            fpSpread1_Sheet1.DataSource = dataSource.GetNWINDData("Customers");

            _tbToSearch.Text = "sales";
        }
        private void _bDoSearch_Click(object sender, EventArgs e)
        {
            int rowIndex = 0;
            int colIndex = 0;
            int cnt = 0;
            int rowCnt = 0;
            int colCnt = 0;
            int startRow = 0;
            int startCol = 0;
            // Clear the listbox
            _lbSrchResult.Items.Clear();
            // Search for the specified item programatically
            rowCnt = (fpSpread1.ActiveSheet.RowCount - 1);
            colCnt = (fpSpread1.ActiveSheet.ColumnCount - 1);

            while (rowIndex != -1 && colIndex != -1 && cnt <= rowCnt)
            {
                // Do Search
                //   returns the matching row(rowIndex) and col number(colIndex) if found
                //   returns -1 if not found
                fpSpread1.Search(fpSpread1.ActiveSheetIndex, _tbToSearch.Text, false, false, false, false, startRow, startCol, rowCnt, colCnt, ref rowIndex, ref colIndex);
                // Add to the listbox
                if(rowIndex!=-1 && colIndex != -1)
                {
                    // Found a match. List it.
                    // NOTE: For display purposes, display the row/col numbers as the user sees them (1 based), 
                    //    not 0 based as Spread uses.  Hence the adding of 1 to the display results
                    _lbSrchResult.Items.Add($"row = {rowIndex+1}, col = {colIndex+1}");
                    cnt = (cnt + 1);
                }
                // Increment the starting row
                if (rowIndex!=-1 && rowIndex!=startRow)
                {
                    // Increment the starting column
                    startRow = (rowIndex + 1);
                    startCol = 0;
                }
                else if (colIndex!=-1 && rowIndex==startRow)
                {
                    startCol = (colIndex + 1);
                }                
            }
            //Display message if no item found
            if (cnt == 0)
                _lbSrchResult.Items.Add("No Matches Found");
        }
    }
}


//
//Introduction.Designer.cs
//


namespace Spread.Search
{
    partial class Introduction
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread1 = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread1_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this._grpSearch = new System.Windows.Forms.GroupBox();
            this._lbSrchResult = new System.Windows.Forms.ListBox();
            this._bSearch = new System.Windows.Forms.Button();
            this._tbToSearch = new System.Windows.Forms.TextBox();
            this._lToSearch = new System.Windows.Forms.Label();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).BeginInit();
            this._grpSearch.SuspendLayout();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread1);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this._grpSearch);
            // 
            // fpSpread1
            // 
            this.fpSpread1.AccessibleDescription = "";
            this.fpSpread1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread1.Location = new System.Drawing.Point(0, 0);
            this.fpSpread1.Name = "fpSpread1";
            this.fpSpread1.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread1_Sheet1});
            this.fpSpread1.Size = new System.Drawing.Size(477, 487);
            this.fpSpread1.TabIndex = 0;
            // 
            // fpSpread1_Sheet1
            // 
            this.fpSpread1_Sheet1.Reset();
            this.fpSpread1_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread1_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread1_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // _grpSearch
            // 
            this._grpSearch.Controls.Add(this._lbSrchResult);
            this._grpSearch.Controls.Add(this._bSearch);
            this._grpSearch.Controls.Add(this._tbToSearch);
            this._grpSearch.Controls.Add(this._lToSearch);
            this._grpSearch.Dock = System.Windows.Forms.DockStyle.Top;
            this._grpSearch.Location = new System.Drawing.Point(0, 0);
            this._grpSearch.Name = "_grpSearch";
            this._grpSearch.Size = new System.Drawing.Size(157, 218);
            this._grpSearch.TabIndex = 1;
            this._grpSearch.TabStop = false;
            this._grpSearch.Text = "Search Programatically";
            // 
            // _lbSrchResult
            // 
            this._lbSrchResult.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this._lbSrchResult.FormattingEnabled = true;
            this._lbSrchResult.Location = new System.Drawing.Point(10, 104);
            this._lbSrchResult.Name = "_lbSrchResult";
            this._lbSrchResult.ScrollAlwaysVisible = true;
            this._lbSrchResult.Size = new System.Drawing.Size(141, 95);
            this._lbSrchResult.TabIndex = 3;
            // 
            // _bSearch
            // 
            this._bSearch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this._bSearch.Location = new System.Drawing.Point(29, 74);
            this._bSearch.Name = "_bSearch";
            this._bSearch.Size = new System.Drawing.Size(103, 23);
            this._bSearch.TabIndex = 2;
            this._bSearch.Text = "Search";
            this._bSearch.UseVisualStyleBackColor = true;
            this._bSearch.Click += new System.EventHandler(this._bDoSearch_Click);
            // 
            // _tbToSearch
            // 
            this._tbToSearch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this._tbToSearch.Location = new System.Drawing.Point(10, 48);
            this._tbToSearch.Name = "_tbToSearch";
            this._tbToSearch.Size = new System.Drawing.Size(141, 20);
            this._tbToSearch.TabIndex = 1;
            // 
            // _lToSearch
            // 
            this._lToSearch.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
            this._lToSearch.AutoSize = true;
            this._lToSearch.Location = new System.Drawing.Point(7, 20);
            this._lToSearch.Name = "_lToSearch";
            this._lToSearch.Size = new System.Drawing.Size(176, 13);
            this._lToSearch.TabIndex = 0;
            this._lToSearch.Text = "Please enter the search term below:";
            // 
            // Introduction
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "Introduction";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread1_Sheet1)).EndInit();
            this._grpSearch.ResumeLayout(false);
            this._grpSearch.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread1;
        private FarPoint.Win.Spread.SheetView fpSpread1_Sheet1;
        private System.Windows.Forms.GroupBox _grpSearch;
        private System.Windows.Forms.Button _bSearch;
        private System.Windows.Forms.TextBox _tbToSearch;
        private System.Windows.Forms.Label _lToSearch;
        private System.Windows.Forms.ListBox _lbSrchResult;
    }
}
